<?php

namespace App\Console\Commands;

use App\Common\Constant\Params;
use App\Models\Goods;
use App\Models\Master;
use App\Models\MasterDetail;
use App\Models\MasterStatistic;
use App\Models\MasterWallet;
use App\Models\MerchantOrderNum;
use App\Models\OldGoods;
use App\Models\OldMaster;
use App\Models\OldMerchant;
use App\Models\Province;
use App\Services\IAreaService;
use App\Services\IMasterService;
use App\Services\IStorageService;
use DB;
use Illuminate\Console\Command;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Collection;
use Log;

class importGoods extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:old-goods {offset=0}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导入商品';

    private $exceptionHandler;
    private $storageService;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(ExceptionHandler $exceptionHandler,IStorageService $storageService)
    {
        parent::__construct();

        $this->exceptionHandler = $exceptionHandler;
        $this->storageService = $storageService;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $bar = $this->output->createProgressBar(OldGoods::count());
        $offset = $this->argument('offset');

        OldGoods::orderBy('go_id')->chunk(100, function ($goods) use (&$bar,&$offset){
            foreach ($goods as $model) {
                if ($bar->getProgress() < $offset){
                    $this->line("\tGoods [{$model->go_id}] skip");
                    $bar->advance();
                    continue;
                }

                DB::beginTransaction();
                try{
                    $goods = Goods::create([
                        'go_id'=>$model->go_id,
                        'go_name'=>$model->go_name,
                        'go_img'=>$model->go_img ? ($this->storageService->saveUrl('http://www.lebangdaojia.com/upload/'.$model->go_img) ? : '') : '',
                        'go_me_money'=>$model->go_me_money,
                        'go_me_id'=>$model->go_me_id,
                        'go_status'=>$model->go_stutas,
                        'go_type'=>$model->go_type,
                        'go_add_ip'=>$model->go_add_ip,
                        'go_model'=>$model->go_model,
                        'go_priced_type'=>Params::ORDER_TYPE_OFFER,
                    ]);
                    if (!$goods){
                        throw new \Exception('goods create fail');
                    }

                    DB::commit();
                    $this->info("\tGoods [{$model->go_id}] success");
                }catch (\Exception $e){
                    DB::rollBack();
                    $this->error("\tGoods [{$model->go_id}] fail\nerror: ".$e->getMessage()." in ".$e->getFile()."[".$e->getLine()."]\n");
                    Log::error("\tGoods [{$model->go_id}] fail\nerror: ".$e->getMessage()." in ".$e->getFile()."[".$e->getLine()."]\n");
                    $this->exceptionHandler->report($e);
                }

                $bar->advance();
            }
        });

        $bar->finish();

    }
}
